<?php

  namespace app\index\controller;
  use app\index\Controller;
  use think\Db;
  use think\Validate;
  /**
   * @title 请假模块（模块06）
   * @description 接口说明
   * @group 用户
   * @header name:model_id   default:06 desc:模块id
   * @groupremark 已使用错误码：1060001 1060002 1060003 1060004 1060005
   */
   class Leave extends Controller{

     /**
      * @title 提交请假信息
      * @description 接口说明
      * @author 李锐志
      * @url /index/leave/index
      * @param name:token type:string require:1  other: desc:token
      * @param name:user_id type:int require:1  other: desc:user_id
      * @param name:start_time type:string require:1  other: desc:请假开始时间
      * @param name:end_time type:string require:1  other: desc:请假结束时间
      * @param name:reason type:string require:1  other: desc:请假原因
      * @method ~
      * @return error_id: 1060001 校验错误
      * @return error_id: 1060002 提交失败
      * @return error_id: 1060003 时间输入有误
      */
      public function index(){

        //获取数据
        $data = input();

        $validate = new Validate([
          ['token','require','缺少token'],
          ['user_id','require','未指定用户'],
          ['start_time','require','请输入起始时间'],
          ['end_time','require','请输入结束时间'],
          ['reason','require','请输入请假原因']
        ]);

        if(!$validate->check($data)){
          $msg['error_id']="1060001";
          $msg['message']=$validate->getError();
          return $this->error($msg);
        }

        $start = strtotime($data['start_time']);
        $end = strtotime($data['end_time']);

        if($end<$start){
          $msg['message'] = '时间输入有误';
          $msg['error_id'] = '1060003';
          return $this->error($msg);
        }else{
          //验证通过后插入数据
          $result = Db::name('leave')->insert($data);
        }

        if($result==true){
          $msg['message'] = '提交成功';
          return $this->success($msg);
        }else{
          $msg['message'] = '提交失败';
          $msg['error_id'] = '1060002';
          return $this->error($msg);
        }
      }
       /**
        * @title 请假列表
        * @description 接口说明
        * @author 梁敏
        * @url /index/leave/getlist
        * @param name:token type:string require:1  other: desc:token
        * @param name:user_id type:int require:1  other: desc:user_id
        * @param name:page type:int require:0  other: desc:page
        * @method ~
        * @return error_id: 1060004 校验错误 1060005获取失败
        */
       public function getlist(){

           //获取数据
           $data = input();
           $listRows = $this->request->param('numPerPage') ? : config("paginate.list_rows");

           $validate = new Validate([
               ['token','require','缺少token'],
               ['user_id','require','未指定用户'],
           ]);

           if(!$validate->check($data)){
               $msg['error_id']="1060004";
               $msg['message']=$validate->getError();
               return $this->error($msg);
           }
           $list = Db::name('leave')
               ->alias('l')
               ->join('tp_admin_user u', 'u.id=l.user_id', 'left')
               ->field('u.*,l.*')
               ->where('user_id',$data['user_id'])
               ->order('l.id desc')
               ->paginate($listRows, false, ['query' => $this->request->get(),"page"=>input("page")]);
           if($list){
               $msg['list']=$list->items();
               $msg['total']=$list->total();
               $msg['per_page']=$list->listRows();
               $msg['current_page']=$list->currentPage();
               $msg['message']="获取成功";
               return $this->success($msg);
           }else{
               $msg['message'] = '获取失败';
               $msg['error_id'] = '1060005';
               return $this->error($msg);
           }
       }
   }
